package me.szlx.thing.bundle;

import me.szlx.thing.Thing;
import me.szlx.thing.ThingCreator;
import me.szlx.thing.Trait;
import me.szlx.thing.TraitCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractThingBundle<T extends Thing> implements ThingBundle<T> {
    private ThingCreator<T> thingCreator;
    private TraitCreator traitCreator;
    private String resource;
    private List<T> thingList;
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public List<T> getAll() {
        if (thingList == null) {
            thingList = doGetAll(getResource());
        }
        return thingList != null ? thingList : new ArrayList<>();
    }

    protected abstract List<T> doGetAll(String resourceStream);

    protected Trait onCreateTrait(String name, String value) {
        return Trait.create(name, value);
    }

    protected String getDefaultResource() {
        return "things.yml";
    }

    public String getResource() {
        if (resource != null) {
            return resource;
        }
        String defaultRersource = getDefaultResource();
        logger.debug("使用缺省资源：{}", defaultRersource);
        return defaultRersource;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }

    public ThingCreator<T> getThingCreator() {
        return thingCreator;
    }

    public void setThingCreator(ThingCreator<T> thingCreator) {
        this.thingCreator = thingCreator;
    }

    public TraitCreator getTraitCreator() {
        return traitCreator;
    }

    public void setTraitCreator(TraitCreator traitCreator) {
        this.traitCreator = traitCreator;
    }
}
